package com.sgb.kjwl.view.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.VideoEntity;
import com.sgb.kjwl.utils.GlideEngine;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

import static com.sgb.kjwl.view.ui.activity.self.publish.GoodsPublishActivity.VIDEO_ADAPTER_LIST;

public class VideoSelectPopWindow extends BasePopWindow {

    private TextView okView, tvInfo1, tvInfo2, tvInfo3;
    private TextView cancelView;
    private ItemInputTextView itvVideoName;
    private LinearLayout addVideo;
    private RelativeLayout rlVideo;
    private ImageView imgVideo;
    private ImageView deleteTv;
    private List<LocalMedia> mData;
    private InputTextInterface inputTextInterface;
    private VideoEntity videoEntity;
    private int indexVideo;
    private int mRCode;


    public VideoSelectPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_video_select;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        cancelView = holder.getPop().findViewById(R.id.cancel_view);//取消
        okView = holder.getPop().findViewById(R.id.ok_view);//确定
        itvVideoName = holder.getPop().findViewById(R.id.itv_video_name);//视频名称
        addVideo = holder.getPop().findViewById(R.id.add_video);//选择视频
        rlVideo = holder.getPop().findViewById(R.id.rl_video);//选择视频
        imgVideo = holder.getPop().findViewById(R.id.img_video);//视频第一帧图片
        deleteTv = holder.getPop().findViewById(R.id.delete_tv);//删除视频按钮
        tvInfo1 = holder.getPop().findViewById(R.id.tv_info1);//提示语
        tvInfo2 = holder.getPop().findViewById(R.id.tv_info2);//提示语
        tvInfo3 = holder.getPop().findViewById(R.id.tv_info3);//提示语

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(itvVideoName.getContent()) && mRCode == PictureConfig.TYPE_VIDEO) {
                    MToast.showToast(mContext, "请输入视频名称");
                    return;
                }
                if (mData == null || mData.size() == 0) {
                    MToast.showToast(mContext, "请选择视频");
                    return;
                }
                dissMiss();
                videoEntity.setTitle(itvVideoName.getContent());
                if (onResultClick != null) {
                    onResultClick.result(1, videoEntity, indexVideo);
                }
            }
        });
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        addVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PictureSelector.create((Activity) mContext)
                        .openGallery(PictureMimeType.ofVideo())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .theme(R.style.picture_default_style)
                        .compress(true)//是否压缩
                        .minimumCompressSize(10240)//小于10M不压缩
                        .selectionMedia(mData)
                        .forResult(mRCode);
            }
        });
        rlVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PictureSelector.create((Activity) mContext)
                        .openGallery(PictureMimeType.ofVideo())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .theme(R.style.picture_default_style)
                        .compress(true)//是否压缩
                        .minimumCompressSize(10240)//小于10M不压缩
                        .selectionMedia(mData)
                        .forResult(mRCode);
            }
        });
        deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rlVideo.setVisibility(View.GONE);
                addVideo.setVisibility(View.VISIBLE);
                mData.clear();
            }
        });
    }

    public void setData(List<LocalMedia> mData, int indexVideo) {
        this.mData = mData;
        this.indexVideo = indexVideo;
        rlVideo.setVisibility(View.VISIBLE);
        addVideo.setVisibility(View.GONE);
        //设置视频第一帧图片
        videoEntity = new VideoEntity();
        if (mData.get(0).getRealPath()==null){
            videoEntity.setUrl(mData.get(0).getPath());//拍摄地址
            imgVideo.setImageBitmap(setVideoFirstImg(mData.get(0).getPath()));
        }else {
            videoEntity.setUrl(mData.get(0).getRealPath());//选择本地地址
            imgVideo.setImageBitmap(setVideoFirstImg(mData.get(0).getRealPath()));
        }
    }

    public void setDataRest(int mRCode) {
        this.mRCode = mRCode;
        itvVideoName.setVisibility(mRCode == VIDEO_ADAPTER_LIST ? View.GONE : View.VISIBLE);
        tvInfo1.setText(mRCode == VIDEO_ADAPTER_LIST ? "视频尺寸：建议上传1:1 或 16:9比例视频" : "视频尺寸：上传1:1 或 16:9比例视频");
        tvInfo2.setText(mRCode == VIDEO_ADAPTER_LIST ? "视频大小：建议≤300MB" : "视频大小：≤100MB");
        tvInfo3.setText(mRCode == VIDEO_ADAPTER_LIST ? "时长：建议≤60s，推荐30秒内短视频" : "时长：≤60s，推荐30秒内短视频");
        if (mData != null)
            mData.clear();
        rlVideo.setVisibility(View.GONE);
        addVideo.setVisibility(View.VISIBLE);
        videoEntity = null;
        itvVideoName.setContent("");
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }

    private Bitmap setVideoFirstImg(String url) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(url);
        Bitmap bitmap = media.getFrameAtTime();
        return bitmap;
    }
}
